using System;
using System.Net;
using System.IO;

namespace WindowsApplication4
{
	/// <summary>
	/// Summary description for CHttpEngine.
	/// </summary>
	public class CHttpEngine
	{
        /// <summary>
        /// Default constructor
        /// </summary>
		public CHttpEngine()
		{
		}

        public StreamReader POSTData(string sURL, string sData)
        {
            //Format the url so that it starts with http:// and does not have any excess spaces
            if (!sURL.StartsWith("http://"))
            {
                sURL = "http://" + sURL.Trim();
            }
            else
            {
                sURL = sURL.Trim();
            }

            //Create an http response and request
            HttpWebResponse myResponse = null;
            HttpWebRequest myRequest = null;


            // Initialize the WebRequest.
            myRequest = (HttpWebRequest)WebRequest.Create(sURL);
            myRequest.Method = "POST";
            myRequest.ContentType = "application/X-www-form-urlencoded";
            myRequest.ContentLength = sData.Length;

            //Create a new stream writer with the http request stream
            StreamWriter swWriter = new StreamWriter(myRequest.GetRequestStream());
            //Write the data to the http request stream
            swWriter.Write(sData);
            swWriter.Close();

            try
            {
                //Try to get the http response
                myResponse = (HttpWebResponse)myRequest.GetResponse();
            }
            catch (WebException ex)
            {
                //If response failed, throw exception
                throw ex;
            }

            //Create a stream reader to read the http response
            StreamReader srReader = null;
            //If response isn't null
            if (myResponse != null)
            {
                //Create stream reader from response
                srReader = new StreamReader(myResponse.GetResponseStream());
            }

            //Return response to http request
            return srReader;
        }

        public StreamReader GETData(string sURL, string sURLData)
        {
            //Format the url so that it starts with http:// and doesn't have excess spaces
            if (!sURL.StartsWith("http://"))
            {
                sURL = "http://" + sURL.Trim();
            }
            else
            {
                sURL = sURL.Trim();
            }

            //Create an http request and response
            HttpWebResponse myResponse = null;
            HttpWebRequest myRequest = null;

            //If data isn't null or empty
            if (sURLData != null && sURLData != "")
            {
                //Add data to end of url, separated by a ?
                sURL = sURL + "?" + sURLData;
            }

            // Return the response. 
            try
            {
                // Initialize the WebRequest.
                myRequest = (HttpWebRequest)WebRequest.Create(sURL);
                myRequest.Timeout = 1000000;

                //If using a proxy
                if (WindowsApplication4.Properties.Settings.Default.useProxy)
                {
                    //Create a webproxy with given settings and set request to use proxy
                    WebProxy myProxy = new WebProxy();
                    myProxy.Address = new Uri(WindowsApplication4.Properties.Settings.Default.proxyAddress);
                    myProxy.UseDefaultCredentials = true;
                    myRequest.Proxy = myProxy;
                }

                //Get the http response
                myResponse = (HttpWebResponse)myRequest.GetResponse();
            }
            catch (Exception ex)
            {
                //Throw any exception that happens
                throw ex;
            }

            StreamReader srReader = null;
            //If http response isn't null
            if (myResponse != null)
            {
                //Marko: change for gzip stream
                System.IO.Compression.GZipStream gz =
                    new System.IO.Compression.GZipStream(myResponse.GetResponseStream(),
                                                         System.IO.Compression.CompressionMode.Decompress);
                //Create a new stream reader from the gz stream
                srReader = new StreamReader(gz);
                //sr = new System.IO.StreamReader(myResponse.GetResponseStream());
            }
            //Return the stream reader of the gz stream
            return srReader;
        }
	}
}
